var $ = require('../../common/helper.js');
Page({
	data: {
		data: [],
		offset: 0,
		loadmore: '',
		status: 1,
		status1_this: 'this',
		status_1_this: ''
	},
	onLoad: function (options) {
		$.overload(false);
		var _this = this, status = options.status ? options.status : 1;
		this.setData({
			status: status
		});
		this.setDelBtn();
	},
	selectStatus: function (e) {
		var _this = this, status = e.currentTarget.dataset.status;
		this.setData({
			status: status
		});
		this.getData(false, function () {
			switch (status) {
				case '1':
					_this.setData({
						status0_this: '',
						status1_this: 'this',
						status_1_this: ''
					});
					break;
				case '-1,-2':
					_this.setData({
						status0_this: '',
						status1_this: '',
						status_1_this: 'this'
					});
					break;
				default:
					_this.setData({
						status0_this: 'this',
						status1_this: '',
						status_1_this: ''
					});
					break;
			}
		});
	},
	onShow: function () {
		wx.startPullDownRefresh();
	},
	onPullDownRefresh: function () {
		this.getData();
	},
	onReachBottom: function () {
		this.getData(true);
	},
	//获取数据开始
	getData: function (isReachBottom, callback) {
		wx.showNavigationBarLoading();
		var _this = this, data = this.data.data, offset = this.data.offset;
		if (!isReachBottom) {
			data = [];
			offset = 0;
		}
		$.get('api.php?app=member&act=order', { offset: offset, status: this.data.status }, function (json) {
			wx.stopPullDownRefresh();
			wx.hideNavigationBarLoading();
			if ($.isArray(json.data)) {
				data = data.concat(json.data);
				offset += json.data.length;
			}
			for (var i in data) {
				data[i].transform = 0;
				data[i].style = '';
			}
			var loadmore = '';
			if (_this.data.offset == offset || data.length < 6) {
				loadmore = 'loadmore-nomore';
			}
			_this.setData({
				data: data,
				offset: offset,
				loadmore: loadmore
			});
			if ($.isFunction(callback)) callback();
		});
	},
	//拖曳删除开始
	setDelBtn: function () {
		this.setData({
			delBtnWidth: this.getEleWidth(180),
			windowWidth: wx.getSystemInfoSync().windowWidth,
			lastTranssform: 0
		});
	},
	getEleWidth: function (width) {
		var real = 0;
		try {
			var windowWidth = wx.getSystemInfoSync().windowWidth;
			var scale = (750 / 2) / (width / 2);
			real = Math.floor(windowWidth / scale);
			return real;
		} catch (e) {
			return 0;
		}
	},
	delItem: function (e) {
		var _this = this,
			data = this.data.data,
			index = e.target.dataset.index;
		$.get('api.php?app=member&act=order_delete', { id: this.data.data[index].id }, function (json) {
			data.splice(index, 1);
			_this.setData({
				data: data
			});
		});
	},
	tap: function (e) {
		var data = this.data.data,
			url = e.currentTarget.dataset.url,
			curIndex = this.getEditing();
		if (curIndex == -1) {
			wx.navigateTo({
				url: url,
			});
		} else {
			data[curIndex].transform = 0;
			data[curIndex].style = 'transform:translate3d(0,0,0)';
			this.setData({
				data: data
			});
		}
	},
	getEditing: function () {
		var data = this.data.data, i = -1;
		for (var index in data) {
			if (data[index].transform != 0) {
				i = index;
				break;
			}
		}
		return i;
	},
	touchStart: function (e) {
		var data = this.data.data,
			index = e.currentTarget.dataset.index,
			curIndex = this.getEditing();
		if ((curIndex > -1 && index != curIndex) || e.touches.length != 1) {
			data[curIndex].style = 'transform:translate3d(0,0,0)';
			this.setData({
				data: data
			});
			setTimeout(function () {
				data[curIndex].transform = 0;
				this.setData({
					data: data
				});
			}.bind(this), 1000);
			return;
		}
		this.setData({
			touchStart: true,
			startX: e.touches[0].clientX
		});
	},
	touchMove: function (e) {
		if (!this.data.touchStart || e.touches.length != 1) return;
		var delBtnWidth = this.data.delBtnWidth,
			data = this.data.data,
			index = e.currentTarget.dataset.index,
			transform = data[index].transform,
			style = '',
			moveX = e.touches[0].clientX,
			disX = this.data.startX - moveX + transform;
		if (disX <= 0) {
			style = 'transform:translate3d(0,0,0)';
		} else {
			if (moveX <= (this.data.windowWidth - delBtnWidth) / 3 * 2 - delBtnWidth) {
				disX = this.data.lastTranssform + (-moveX + (this.data.windowWidth - delBtnWidth) / 3 * 2 - delBtnWidth) * 0.2;
			} else {
				this.setData({
					lastTranssform: disX
				});
			}
			style = 'transform:translate3d(-' + disX + 'px,0,0);transition-duration:0s';
		}
		data[index].style = style;
		this.setData({
			data: data
		});
	},
	touchEnd: function (e) {
		if (!this.data.touchStart || e.changedTouches.length != 1) return;
		var delBtnWidth = this.data.delBtnWidth,
			data = this.data.data,
			index = e.currentTarget.dataset.index,
			transform = data[index].transform,
			style = '',
			endX = e.changedTouches[0].clientX,
			disX = this.data.startX - endX + transform;
		if (disX < delBtnWidth / 2) {
			data[index].transform = 0;
			style = 'transform:translate3d(0,0,0)';
		} else {
			data[index].transform = delBtnWidth;
			style = 'transform:translate3d(-' + delBtnWidth + 'px,0,0)';
		}
		data[index].style = style;
		this.setData({
			touchStart: false,
			lastTransform: data[index].transform,
			data: data
		});
	}
})